var app = getApp();  //获取app.js
const api = require('../../config/config.js');
Page({

  /**
   * 页面的初始数据
   */
  data:{
    index:1,//分页参数
    url:app.globalData.url,
    navHeight:app.globalData.navHeight,//按照每个页面加,默认不加
    propertyList:[],
    selectedSortId:1,
    categoryList:[]

},
selectCategory(e){
  let selectedSortId=e.currentTarget.id;
  //根据id发起请求刷新propertyList----------------------------------
  //=1为查询全部
  if (selectedSortId==1){
    let params = { index:0};
    this.getPropertyList(params);
   }else{
   //按类目查询
   let params = { index:0,sortId:selectedSortId};
   this.getPropertyList(params);
   }
  this.setData({
    selectedSortId
  })
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(this.data.navHeight);
    let params = { index:0};
   this.getPropertyList(params);
   //获取分类列表
   let that=this;
   wx.request({
     url: api.getSortListUrl,
     method: 'POST',
     data:params,
     header: {
       'content-type': 'application/x-www-form-urlencoded'
     },
     success: function (res) {
       that.setData({
        categoryList:res.data.data
       })
     }
   });
  },
//触底发分页（需判断当前有没有选择类目）
  onReachBottom: function () {
    //下滑提示加载中，然后显示6条数据
    let index=this.data.index;
    wx.showLoading({
      title: '加载中...'
    });
    let selectedSortId=this.data.selectedSortId;
    //=1为没选
  if (selectedSortId==1){
    let params = { index:++index};
    this.getPropertyList(params);
   }else{
   //选择类目的分页加载
   let params = { index:++index,sortId:selectedSortId};
   this.getPropertyList(params);
   }


  },
  //获取文章列表
getPropertyList:function(params){
 //加载6条数据
 let that=this;
 let index=params.index;
 wx.request({
   url: api.getPropertysUrl,
   method: 'POST',
   data:params,
   header: {
     'content-type': 'application/x-www-form-urlencoded'
   },
   success: function (res) {
     if(index==0){
      that.setData({
        //查询功能
        propertyList:[...res.data.data],
        index
      })
     }else{
       //分页功能
      that.setData({
        // 拼接了数组
        propertyList:[...that.data.propertyList,...res.data.data],
        index
      })
     }
     
    wx.hideLoading();
   },
   fail: function (error) {
    wx.hideLoading();
     that.showInfo('请求失败');
   }
 });
},
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})